var Settings = {
		settings : 
        {
            temperatureThreshold: ""
        },
        
		getSettings : function() {
			var uri =  "/gosmart/settings";
			$.ajax({
				type: 'GET',
				url: uri,
				dataType: 'json',
				beforeSend: function(xhr)
				{
					$("#loading").css ("display", "block");
					return true;
				},
				success: function(response) 
				{ 
					$("#loading").css ("display", "none");
					Settings.settings = response.settings;
					Settings.renderSettings();
				},
				error: function(xhr, reason, exception) 
				{             	
					Contacts.renderError("Sorry but the request failed.");
				}
			});
		},
		
		submitTemperatureThreshold : function () {
			var tempThr = document.getElementById("newTempThreshold").value;
			var uri =  "/gosmart/settings?temperatureThreshold=" + tempThr;
			$.ajax({
				type: 'POST',
				url: uri,
				dataType: 'json',
				beforeSend: function(xhr)
				{
					$("#newTempThreshold").css ("display", "block");
				},
				error: function(xhr, reason, exception) 
				{             	
					
				},
				complete: function () 
				{
					Settings.getSettings();
					$("#newTempThreshold").html("");
				}
			});
		},
		
		renderSettings : function () {
			$("#content").html("");
			var html = "";
			html += "<table">";
			html += "<thead>";
			html += "<tr><th>Name</th><th>Value</th><th>New value</th><th></th></tr>";
			html += "</thead>";
			html += "<tbody>";
			html += "<tr><td>TemperatureThreshold</td><td>" + Settings.settings.temperatureThreshold + "</td><td><input type=\"text\" id=\"newTempThreshold\" size=\"8\" /></td><td><button onClick='Settings.submitTemperatureThreshold();'>Submit</button></td></tr>";
			html += "</tbody>"
			html += "</table>";
			$("#content").html(html);
		}
	}

$(document).ready (function () {
	Settings.getSettings();
}); 